package site.madai.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import org.apache.poi.ss.formula.functions.Count;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import site.madai.dao.MemberDao;
import site.madai.pojo.Member;
import site.madai.service.MemberService;
import site.madai.utils.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Project: site.madai.service.Impl
 * @Author: ShaoDi Wang
 * @Date: Created in 2019-07-01 15:05
 * @Description:
 * @Version: 1.0
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    @Override
    @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    @Override
    public Map<String, Object> getMemberDubbleLoopReport() {

        // 获得男性会员数量
        long manMemberCount = memberDao.getManMemberCount();
        // 获得女性会员数量
        long womanMemberCount = memberDao.getWomanMemberCount();

        String startDateStr = DateUtils.getBirthdayByAge(0);
        String endDateStr = DateUtils.getBirthdayByAge(18);
        // 获取年龄在0-18的会员数量
        long Count0_18 = memberDao.getYearoldScopeCount(startDateStr,endDateStr);

        startDateStr = DateUtils.getBirthdayByAge(18);
        endDateStr = DateUtils.getBirthdayByAge(30);
        // 获取年龄在18-30的会员数量
        long Count18_30 = memberDao.getYearoldScopeCount(startDateStr,
                endDateStr);
        startDateStr = DateUtils.getBirthdayByAge(30);
        endDateStr = DateUtils.getBirthdayByAge(45);
        // 获取年龄在30-45的会员数量
        long Count30_45 = memberDao.getYearoldScopeCount(startDateStr,
                endDateStr);
        startDateStr = DateUtils.getBirthdayByAge(45);
        endDateStr = null;
        // 获取年龄在45以上的会员数量
        long Count45 = memberDao.getYearoldScopeCount(startDateStr,endDateStr);
        HashMap<String, Object> map = new HashMap<>();
        map.put("manCount",manMemberCount);
        map.put("womanCount",womanMemberCount);
        map.put("count0_18", Count0_18);
        map.put("count18_30",Count18_30);
        map.put("count30_45",Count30_45);
        map.put("count45up",Count45);
        return map;
    }

    @Override
    @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
    public List<Integer> findMemberCountByMonth(List<String> month) {
        List<Integer> list = new ArrayList<>();
        for (String m : month) {
            //格式：2019.04.31
            m = m + ".31";
            Integer count = memberDao.findMemberCountBeforeDate(m);
            list.add(count);
        }
        return list;
    }

    @Override
    public void add(Member member) {
        memberDao.add(member);
    }

    @Override
    public List<Map<String, Object>> findMemberCount4age() {
        return memberDao.findMemberCount4age();
    }

    @Override
    public List<Map<String, Object>> findMemberCount4sex() {
        return memberDao.findMemberCount4sex();
    }
}
